﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.util;
using System.Xml.Linq;

namespace PocoReport
{

    public class Style
    {
        [Browsable(false)]
        public Properties Properties { get; protected set; }

        public Style()
        {
            Properties = new Properties();
        }

        public Style(XElement element): this()
        {
            string style = element.GetAttribValue("Style", null);
            if(!string.IsNullOrEmpty(style))
            {
                string[] nameValues = style.ToLowerInvariant().Split(new []{";"}, StringSplitOptions.RemoveEmptyEntries);
                foreach (string nameValue in nameValues)
                {
                    string[] pair = nameValue.Split(new [] {':'}, 2);
                    if(pair.Length == 2)
                    {
                        Properties.Add(pair[0].Trim(), pair[1].Trim());
                    }
                }
            }
        }

        public static Style Merge(Style style1, Style style2)
        {
            Properties properties = new Properties();
            properties.AddAll(style1.Properties);
            properties.AddAll(style2.Properties);
            return new Style{Properties = properties};
        }

        public override string ToString()
        {
            StringBuilder builder = new StringBuilder();
            bool first = true;
            foreach (DictionaryEntry property in Properties)
            {
                if(!first)
                {
                    builder.Append("; ");
                }
                first = false;
                builder.Append(property.Key);
                builder.Append(": ");
                builder.Append(property.Value);
            }
            return builder.ToString();
        }
    }
}
